package pw.hais.main.main_app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

import pw.hais.main.R;
import pw.hais.main.main_app.app.BaseActivity;
import pw.hais.main.main_app.fragment.DrawerMenuFragment;
import pw.hais.utils.L;

/**
 * Created by hais1992 on 2015/12/13.
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {
    private ImageButton btn_clown, btn_meinv, btn_chat;
    private DrawerLayout layout_drawer;
    private ActionBarDrawerToggle drawerToggle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);


        initLayoutView();
        initActionBar();
    }

    private void initLayoutView() {
        btn_clown = (ImageButton) findViewById(R.id.btn_clown);
        btn_meinv = (ImageButton) findViewById(R.id.btn_meinv);
        btn_chat = (ImageButton) findViewById(R.id.btn_chat);
        btn_clown.setOnClickListener(this);
        btn_meinv.setOnClickListener(this);
        btn_chat.setOnClickListener(this);
    }

    //初始化状态栏
    private void initActionBar() {
        layout_drawer = (DrawerLayout) findViewById(R.id.layout_drawer);
        drawerToggle = new ActionBarDrawerToggle(this,layout_drawer,R.string.drawer_open,R.string.drawer_close);
        drawerToggle.syncState();
        layout_drawer.setDrawerListener(drawerToggle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.drawer_fragment_view, new DrawerMenuFragment())
                .commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_author) {
            startActivity(new Intent(context, AuthorActivity.class));
            return true;
        }else if (id == 16908332) { //菜单
            if (!layout_drawer.isDrawerOpen(Gravity.LEFT))layout_drawer.openDrawer(Gravity.LEFT);
            else layout_drawer.closeDrawer(GravityCompat.START);
            return true;
        }else{
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_clown:    //搞笑栏目
                startActivity(new Intent(context, HaiHanActivity.class));
                break;
            case R.id.btn_meinv:    //镁铝栏目
//                startActivity(new Intent(context, pw.hais.main.plug_meinv.activity.WelcomeActivity.class));
                break;
            case R.id.btn_chat:  //历史今天
                startActivity(new Intent(context, ChatRoomActivity.class));
                break;
        }
    }

    private long outTime=0;	//用于 两次退出键，退出软件
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // 监听菜单键
        switch (keyCode) {
            case KeyEvent.KEYCODE_MENU:
                if (!layout_drawer.isDrawerOpen(Gravity.LEFT))layout_drawer.openDrawer(Gravity.LEFT);
                else layout_drawer.closeDrawer(GravityCompat.START);
                break;
            case KeyEvent.KEYCODE_BACK:
                long nowTime = System.currentTimeMillis() / 1000;
                if(nowTime - outTime <= 2 ){
                    finish();
                    app.exitApp();
                }else{
                    L.showShort("再次点击返回退出 " + getResources().getString(R.string.app_name) + " .");
                    outTime = nowTime;
                }
                return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(app.user==null){
            startActivity(new Intent(context,StartActivity.class));
            finish();
        }
    }
}
